﻿@using PagedList.Mvc;

@model PagedList.IPagedList<Product>

@section styles{
    @Styles.Render("~/Content/PagedList.css")
}

@{
    Category category = ViewBag.Category;
    Category currentCategory = ViewBag.CurrentCategory;
    List<PropertyType> propertyTypes = ViewBag.PropertyTypes;
    propertyTypes.Insert(0, new PropertyType { NameId = "brand", Name = "Brand", NameVn = "Thương hiệu" });
    string sortOrder = ViewBag.SortOrder;

    ViewBag.MenuOneId = category.ParentId;
    ViewBag.Title = String.Format("{0} - {1}", category.Parent.Name, category.Name);
}

<div id="product" class="index">
    <div class="container">
        <div class="bg-content">
            <div class="form-filter">
                <img class="img-category hidden-xs" src="@(category.ImageUrl ?? "/img/no-image.png?w=250&h=115")" />
                @Html.Action("_CategoryFilter", new { category = category, currentCategory = currentCategory })
                @using (Html.BeginForm("Index", "Product", FormMethod.Get, new { id = "frmProductFilter" }))
                {
                    if (!String.IsNullOrWhiteSpace(Request["Category"]))
                    {
                        @Html.Hidden("Category", Request["Category"])
                    }

                    <div style="clear: both; height: 10px;"></div>

                    if (Model.Count > 0)
                    {
                        @Html.Partial("_Sorting", sortOrder)
                    }

                    foreach (PropertyType propType in propertyTypes)
                    {
                        @Html.Action("_PropertyFilter", new
                        {
                            list = ViewData[propType.NameId],
                            nameId = propType.NameId,
                            title = (propType.NameVn ?? propType.Name)
                        })
                    }
                }
            </div>
        </div>

        <div style="clear: both;"></div>

        @if (Model.Count > 0)
        {
            int index = 0;

            var request = HttpUtility.ParseQueryString(Request.Url.Query);
            request.Remove("page");
            string requestUrl = String.IsNullOrWhiteSpace(request.ToString()) ? "" : "&" + request.ToString();

            <div class="form-pagelist" style="margin-bottom: 10px;">
                <div class="row">
                    <div class="col-sm-4">
                        <div class="well-sm">
                            <span class="badge">@Model.FirstItemOnPage</span>
                            - <span class="badge">@Model.LastItemOnPage</span>
                            / <span class="badge">@Model.TotalItemCount</span>
                        </div>
                    </div>
                    <div class="col-sm-8 text-right">
                        @Html.PagedListPager(Model, page => Url.Action(null, new
                        {
                            page,
                            categoryNameId = category.NameId,
                        }) + requestUrl, new PagedListRenderOptions
                        {
                            LinkToFirstPageFormat = DataTableConstant.LinkToFirstPageFormat,
                            LinkToPreviousPageFormat = DataTableConstant.LinkToPreviousPageFormat,
                            LinkToNextPageFormat = DataTableConstant.LinkToNextPageFormat,
                            LinkToLastPageFormat = DataTableConstant.LinkToLastPageFormat,
                            MaximumPageNumbersToDisplay = 5
                        })
                    </div>
                </div>
            </div>
            <div class="row min-padding">
                @foreach (Product item in Model)
                {
                    index++;

                    <div class="col-md-4 col-sm-6 col-xs-6">
                        @Html.Action("_ProductItem", "Product", new { product = item, lazyLoad = true })
                    </div>

                    if (index % 3 == 0)
                    {
                        <div class="clearfix visible-lg visible-md"></div>
                    }
                    if (index % 2 == 0)
                    {
                        <div class="clearfix visible-sm visible-xs"></div>
                    }
                }
            </div>
            <hr />
            <div class="form-pagelist">
                <div class="row">
                    <div class="col-sm-4">
                        <div class="well-sm">
                            <span class="badge">@Model.FirstItemOnPage</span>
                            - <span class="badge">@Model.LastItemOnPage</span>
                            / <span class="badge">@Model.TotalItemCount</span>
                        </div>
                    </div>
                    <div class="col-sm-8 text-right">
                        @Html.PagedListPager(Model, page => Url.Action(null, new
                        {
                            page,
                            categoryNameId = category.NameId,
                        }) + requestUrl, new PagedListRenderOptions
                        {
                            LinkToFirstPageFormat = DataTableConstant.LinkToFirstPageFormat,
                            LinkToPreviousPageFormat = DataTableConstant.LinkToPreviousPageFormat,
                            LinkToNextPageFormat = DataTableConstant.LinkToNextPageFormat,
                            LinkToLastPageFormat = DataTableConstant.LinkToLastPageFormat,
                            MaximumPageNumbersToDisplay = 5
                        })
                    </div>
                </div>
            </div>
            <br />
        }
        else
        {
            <div class="bg-content">
                <img src="/img/no-event.png" class="center-block img-responsive" />
            </div>
        }
    </div>
</div>

@section scripts{
    <script>
        $('#menuOne-@category.Parent.NameId').addClass('active');
        $('#menuTwo-@category.NameId').addClass('active');
        $('#menuTwoXs-@category.NameId').addClass('active');
        $('.lazy-load').lazyload();

        function AddFilter(self) {
            var nameId = $(self).attr('data-id');
            $(self).find('input').attr('checked', 'checked');
            $("#frmProductFilter").submit();
        }

        function RemoveFilter(self) {
            $(self).find('input').removeAttr('checked');
            $("#frmProductFilter").submit();
        }

        function RemoveAllFilter(self) {
            $(self).parent().find('input').removeAttr('checked');
            $("#frmProductFilter").submit();
        }
    </script>
}